// Chromanova!!!

/*
 * Benjamin Hitov
 * Ricky Mondello
 * Brooke Evans
 * Daniel Millstein
 *
 * BlankArea.java - Part of the level editor.
 *
 * NOTE: A template that could read mouse coordinates from a scolling pane after
 *      a mouse click was used to write the level editor. Much code was borrowed
 */


package chromafinal;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;

public class BlankArea extends JLabel {


    public static final int BALL = 1;
    public static final int TELE = 2;
    public static final int BOOST = 3;
    public static final int WALL = 4;

    public static int levelSize = 5000;

    Dimension minSize = new Dimension(levelSize, 500);
    ArrayList<LevelObject> balls;
    int done;
    int xt, yt, xt2, yt2;

    public void undo(){

        if (balls.size() == 0)
        {
            System.out.println("No steps to undo.");
            return;
        }
	balls.remove( balls.size()-1 );
        paint();
    }

    public BlankArea(Color color) {
        balls = new ArrayList();
        setBackground(color);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
        done = 0;
        xt = 0; yt = 0;
    }

    public Dimension getMinimumSize() {
        return minSize;
    }

    public Dimension getPreferredSize() {
        return minSize;
    }

    public static LevelObject makeObject2(String[] params)
    {
        int npars = LevelObject.NPARS[Integer.parseInt(params[0])];
        int[] pars = new int[npars+1];
        

        for (int i = 0; i <= npars; i++)
        {
            pars[i] = Integer.parseInt(params[i]);
        }

        if (pars[0] == BALL)
            return new Ball(pars[1], pars[2], pars[3]);
        else if (pars[0] == TELE)
            return new TeleportIn(pars[1], pars[2], pars[3], pars[4], pars[5]);
        else if (pars[0] == BOOST)
        {
            int [] bvars = new int[6];
            for (int i = 1; i < 7; i++)
                bvars[i-1] = pars[i];


            return new Boost(bvars, false, pars[7]);
        }
        else
            return new Wall(pars[1],pars[2],pars[3],pars[4],pars[5]);

    }

    public void makeObject(int x, int y, int p1, int p2, int p3)
    {
        if (p2 == 1)
        {
            makeBall(x,y,p1);
        }
        else if (p2 == 2)
            if (done == 0)
            {
                done = 1;
                xt = x; yt = y;
            }
            else
            {
                done = 0;
                makeTele(xt,yt,x,y,p3);
            }
        else if (p2 == 3)
            if (done == 0)
            {
                done = 2;
                xt = x; yt = y;
            }
            else if (done == 2)
            {
                done = 1;
                xt2 = x; yt2 = y;
            }
            else
            {
                done = 0;
                makeBoost(xt,yt,xt2,yt2,x,y,p3);
            }
            else if (p2 == 4)
            {
                if (done == 0)
                {
                    done = 1;
                    xt = x; yt = y;
                }
                else
                {
                    done = 0;
                    if (Math.abs(x-xt) > Math.abs(y-yt))
                        makeWall(xt,yt,x,yt,p3);
                    else
                        makeWall(xt,yt,xt,y,p3);
                }
            }
    }

    public void makeWall(int xt, int yt, int x, int y, int width)
    {
        Wall tmp = new Wall(xt,yt,x,y,width);
        tmp.draw(getGraphics());
        balls.add(tmp);
    }

    public void makeBoost(int xt, int yt, int xt2, int yt2, int x, int y, int force)
    {
        Boost tmp = new Boost(new int[] {xt,yt,xt2,yt2,x,y},false,force);
        tmp.draw(getGraphics());
        balls.add(tmp);
    }

    public void makeBall(int x, int y, int mass)
    {
        Ball tmp = new Ball(x,y,mass);
        tmp.draw(getGraphics());
        balls.add(tmp);
    }

    public void makeTele(int x, int y, int xo, int yo, int size)
    {
        TeleportIn tmp = new TeleportIn(x,y,xo,yo,size);
        tmp.draw(getGraphics());
        balls.add(tmp);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        for (int i = 0; i < balls.size(); i++)
        {
            (balls.get(i)).draw(getGraphics());
        }
    }

    public void paint()
    {
        Graphics g = getGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,levelSize,500);
        for (int i = 0; i < balls.size(); i++)
        {
            ( balls.get(i)).draw(getGraphics());
        }
    }

    public void save(String filename){

	StringBuilder s = new StringBuilder();
	for (int i=0; i< balls.size(); i++){
	    s.append( balls.get(i).makeText() );
	}
	try{
	    PrintWriter out = new PrintWriter(filename);
	    out.write(s.toString());
            out.close();
	}catch(Exception e){
	    System.err.println("Derp file...");
	}

    }



    public static ArrayList<Ball> load2(String filename, int waitsize){
	ArrayList<Ball> bb = new ArrayList();
	File file = new File(filename);
	try{
	    Scanner input = new Scanner(file);
	    while(input.hasNext()) {
		String nextLine = input.nextLine();
		String [] temp = null;
		temp = nextLine.split("\t");
		Ball awesome = new Ball(Integer.parseInt(temp[0]) + waitsize,
                        Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));
		bb.add(awesome);
	    }
	    input.close();
	    return bb;

	}catch (IOException e){
	    System.err.println ("Unable to read from file");
	}
	return null;
    }

    public static ArrayList<LevelObject> load(String filename, int waitsize){
	ArrayList<LevelObject> bb = new ArrayList();
	File file = new File(filename);
	try{
	    Scanner input = new Scanner(file);
	    while(input.hasNext()) {
		String nextLine = input.nextLine();
		String [] temp = null;
		temp = nextLine.split("\t");
                bb.add(makeObject2(temp));
		
	    }
	    input.close();
	    return bb;

	}catch (IOException e){
	    System.err.println ("Unable to read from file");
	}
	return null;
    }
}
